package com.datawin.taotaocheche.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.jakewharton.rxbinding.view.RxView;
import com.datawin.taotaocheche.R;
import com.datawin.taotaocheche.base.BaseFragment;
import com.datawin.taotaocheche.bean.ClueInfo;
import com.datawin.taotaocheche.event.NetErrorEvent;
import com.datawin.taotaocheche.event.RechargeEvent;
import com.datawin.taotaocheche.ui.adapter.OwnAdapter;
import com.datawin.taotaocheche.ui.fragment.biz.OwnerBiz;
import com.datawin.taotaocheche.ui.fragment.component.DaggerOwnerComponent;
import com.datawin.taotaocheche.ui.fragment.contract.OwnerContract;
import com.datawin.taotaocheche.ui.fragment.module.OwnerMoudle;
import com.datawin.taotaocheche.ui.fragment.presenter.OwnerPresent;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by chenjian on 17/11/30.
 */

public class OwnerFrag extends BaseFragment implements OwnerContract.View ,SuperRecyclerView.LoadingListener{

    @Bind(R.id.super_recycler_view)
    SuperRecyclerView superRecyclerView;

    @Inject
    OwnerPresent presenter;
    @Inject
    OwnerBiz biz;
    OwnAdapter adapter;

    @Bind(R.id.layout_no_data_gone)
    LinearLayout layout;
    @Bind(R.id.clue_select)
    FrameLayout select;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_owner, container, false);
        ButterKnife.bind(this, view);
        DaggerOwnerComponent.builder().ownerMoudle(new OwnerMoudle(this)).build().inject(this);
        presenter.setBiz(biz);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        superRecyclerView.setLayoutManager(layoutManager);
        superRecyclerView.setRefreshEnabled(true);// 可以定制是否开启下拉刷新
        superRecyclerView.setLoadMoreEnabled(true);// 可以定制是否开启加载更多
        superRecyclerView.setLoadingListener(this);// 下拉刷新，上拉加载的监听
        superRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);// 下拉刷新的样式
        superRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);// 上拉加载的样式
        superRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);// 设置下拉箭头
        presenter.getList();
        RxView.clicks(select)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.toExport();
                    }
                });
        return view;
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.unbind(this);
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }


    @Override
    public void setNoDate(boolean b) {
        if(b){
            layout.setVisibility(View.VISIBLE);
        }else{
            layout.setVisibility(View.GONE);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetEvent(RechargeEvent info) {
        presenter.refresh();
        presenter.getList();
    }


    @Override
    public void onRefresh() {
        presenter.refresh();
        presenter.getList();
    }

    @Override
    public void onLoadMore() {
        presenter.getList();
    }

    @Override
    public void updateCollection(List<ClueInfo> coupons) {
        if (adapter == null) {
            adapter = new OwnAdapter(getActivity(), coupons);
            superRecyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        superRecyclerView.completeRefresh();
    }

    @Override
    public void loadMoreComplete() {
        superRecyclerView.completeLoadMore();
    }

    @Override
    public void hasLoadMore(boolean b) {
        superRecyclerView.setNoMore(b);
    }
}
